package come.demo.widgets.ui;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

import come.demo.widgets.R;
import come.demo.widgets.utils.Utils;
import tzy.imageloader.BaseImageLoader;
import tzy.imageloader.ImageLoader;
import tzy.pictureview.MyImageView;


public class PictureActivity extends Activity {

    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_browse);
        mViewPager = (ViewPager) findViewById(R.id.view_pager);
        new ImageTask().execute();

    }

    public static void gotoActivity(Context context) {
        Intent intent = new Intent(context, PictureActivity.class);
        context.startActivity(intent);
    }

    class ImageTask extends AsyncTask<Void, Void, List<String>> {
        @Override
        protected List<String> doInBackground(Void... params) {
            final List<String> paths = new ArrayList<>();
            if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {

                Uri originalUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                ContentResolver cr = PictureActivity.this.getApplicationContext().getContentResolver();
                Cursor cursor = cr.query(originalUri, null, null, null, null);
                if (cursor == null) {
                    return paths;
                }
                for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                    String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
                    paths.add(path);
                }
                cursor.close();

            }
            return paths;
        }

        @Override
        protected void onPostExecute(List<String> strings) {
            mViewPager.setAdapter(new ImageAdapter(PictureActivity.this, new MyImageLoader(PictureActivity.this), strings));

        }
    }

    private static class MyImageLoader extends BaseImageLoader {

        final int width, height;

        MyImageLoader(Context context) {
            super();
            width = height = (int) (context.getResources().getDisplayMetrics().density * 100);
        }

        @Override
        protected Bitmap getBitmapFormUrl(String url) {
            return Utils.decodeBitmapFromFile(url, width, height);
        }
    }


    public class ImageAdapter extends PagerAdapter implements View.OnClickListener {
        private final String TAG = getClass().getSimpleName();
        private final Deque<View> mCaches = new ArrayDeque<>(3);
        private final List<String> mUrls;
        private final Context mContext;
        private final ImageLoader mImageLoader;

        public ImageAdapter(Context context, ImageLoader imageLoader, List<String> urls) {
            mContext = context;
            mImageLoader = imageLoader;
            mUrls = urls;
        }

        protected void setImageView(ImageView view, int position) {
            final String url = mUrls.get(position);
            mImageLoader.loadImage(view, url);

        }


        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = mCaches.pollFirst();
            if (view == null) {
                view = new MyImageView(mContext);
                view.setOnClickListener(this);
            }
            container.addView(view);
            setImageView((ImageView) view, position);
            return view;
        }

        @Override
        public int getCount() {
            return mUrls.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            View view = (View) object;
            container.removeView(view);
            mCaches.offerLast(view);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void onClick(View v) {
            ((Activity) mContext).finish();
        }
    }
}
